﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using Microsoft.AspNet.Identity;
using NPOI.HSSF.UserModel;
using OfficeOpenXml;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace GPS.Controllers.GPS_CapPhep
{
    public class GPS_GiayPhepGiaoVienController : Controller
    {
        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        public void initialCategorySearchAction(ViewModelDCPGV viewModel)
        {
            ViewBag.GV_HANGDT_MAs = new SelectList(_db.Filter<SHANGDT>(x => x.HANGDT_RANK >= 5), "HANGDT_MA", "HANGDT_TEN");
            //string DVGTVT_MA = this.getCurrentUser().MA_TINHTP;
            //ViewBag.checkuser = DVGTVT_MA;
            //string getdvgtvt_Ma = GPS.DAL.Check_User.CheckUser(this.getCurrentUser().MA_TINHTP, viewModel.pDCP_GV_DVGTVT_MA);
            //viewModel.pDCP_GV_DVGTVT_MA = getdvgtvt_Ma;
        }

        public GPS.MODELS.ApplicationUser getCurrentUser()
        {
            ApplicationDbContext dbUser = new ApplicationDbContext();
            string userId = User.Identity.GetUserId();
            ApplicationUser _currentUser = dbUser.Users.Find(userId);
            return _currentUser;
        }

        #region index
        public ActionResult Index(ViewModelDCPGV SearchModel, int? currentPage)
        {
            initialCategorySearchAction(SearchModel);
            List<CrudModelDCPGV> model = new List<CrudModelDCPGV>();
            string DVGTVT_MA = this.getCurrentUser().MA_TINHTP;
            ViewBag.checkuser = DVGTVT_MA;
            string getdvgtvt_Ma = GPS.DAL.Check_User.CheckUser(this.getCurrentUser().MA_TINHTP, SearchModel.pDCP_GV_DVGTVT_MA);
            SearchModel.pDCP_GV_DVGTVT_MA = getdvgtvt_Ma;

            //if ((!string.IsNullOrEmpty(SearchModel.pDCP_GV_DVGTVT_MA) && SearchModel.ExportButton == "Search")
            //    || (DVGTVT_MA == "40" && SearchModel.ExportButton == "Search"))
            //{
            model = Singleton<GPS_DAL.GPS_CapPhepDAO>.Inst.GPS_CAPPHEP_DCPGV(SearchModel.pDCP_GV_DVGTVT_MA
                                                                                            , SearchModel.pDCP_GV_MA
                                                                                            , SearchModel.pDCP_GV_HODEM
                                                                                            , SearchModel.pDCP_GV_TEN
                                                                                            , SearchModel.pDCP_GV_HANGDT_MA
                                                                                            , SearchModel.pDCP_GV_SOCMT);
            Int16 icout = 1;
            foreach (var currentEntity in model)
            {
                currentEntity.STT = icout.ToString();
                icout++;
            }
            ViewBag.TotalRow = model.Count();
            //}

            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);
            return View(SearchModel);
        }
        #endregion

        #region Detail
        public ActionResult _Detail(string id1, string id2)
        {
            string _GV_MA = id1;
            string _GV_DVGTVT_MA = id2;

            CrudModelDCPGV model = Singleton<GPS_DAL.GPS_CapPhepDAO>.Inst.GPS_CAPPHEP_CNGVDLX(_GV_DVGTVT_MA, _GV_MA);
            return PartialView("_Detail", model);
        }
        public CrudModelDGV CopyObject_model(DGV model)
        {
            CrudModelDGV _model = new CrudModelDGV
            {
                GV_MA = model.GV_MA,
                GV_DVGTVT_MA = model.GV_DVGTVT_MA,
                GV_HANGDT_MA = model.GV_HANGDT_MA,
                GV_HODEM = model.GV_HODEM,
                GV_TEN = model.GV_TEN,
                GV_NGAYSINH = model.GV_NGAYSINH,
                GV_SOCMT = model.GV_SOCMT,
                GV_NOICT = model.GV_NOICT,
                GV_NOICT_DVHC_MA = model.GV_NOICT_DVHC_MA,
                GV_NOICT_DVGTVT_MA = model.GV_NOICT_DVGTVT_MA,
                GV_GIOITINH = model.GV_GIOITINH,
                GV_TEL = model.GV_TEL,
                GV_HTTUYENDUNG = model.GV_HTTUYENDUNG,
                GV_TDVANHOA = model.GV_TDVANHOA,
                GV_TDCHUYENMON = model.GV_TDCHUYENMON,
                GV_TDSUPHAM = model.GV_TDSUPHAM,
                GV_HGPLX_MA = model.GV_HGPLX_MA,
                GV_NGAYCAPGPLX = model.GV_NGAYCAPGPLX,
                GV_THAMNIENLX = model.GV_THAMNIENLX,
                GV_SOQD_GCN = model.GV_SOQD_GCN,
                GV_NGAYQD_GCN = model.GV_NGAYQD_GCN,
                GV_HTDTAO_MA = model.GV_HTDTAO_MA,
                GV_CAUTAOSUACHUA = (model.GV_CAUTAOSUACHUA == "0" ? false : true),
                GV_DAODUCLAIXE = (model.GV_DAODUCLAIXE == "0" ? false : true),
                GV_NGHIEPVUVANTAI = (model.GV_NGHIEPVUVANTAI == "0" ? false : true),
                GV_LUATGTDB = (model.GV_LUATGTDB == "0" ? false : true),
                GV_KYTHUATLAIXE = (model.GV_KYTHUATLAIXE == "0" ? false : true),
                GV_MOTA = model.GV_MOTA,
                GV_ACTIVE = (model.GV_ACTIVE == "0" ? false : true),
                GV_NGUOINHAP = "SYSTEM",
                GV_ANHCD = model.GV_ANHCD
            };
            return _model;
        }
        public void initialCategoryDetailAction(CrudModelDGV view)
        {
            if (view.GV_ACTIVE == true)
            {
                ViewBag.txtTrangThai = "Hiệu Lực";
            }
            else
            {
                ViewBag.txtTrangThai = "Không Hiệu Lực";
            }

            ViewBag.GV_DVGTVT_MAs = _db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"
                                                            && o.DVGTVT_MA == view.GV_DVGTVT_MA).Select(x => x.DVGTVT_TEN).FirstOrDefault();
            ViewBag.GV_HANGDT_MAs = _db.Filter<SHANGDT>(o => o.HANGDT_MA == view.GV_HANGDT_MA).Select(x => x.HANGDT_TEN).FirstOrDefault();
            ViewBag.GV_HGPLX_MAs = _db.Filter<SHANGGPLX>(o => o.HGPLX_MA == view.GV_HGPLX_MA).Select(x => x.HGPLX_TEN).FirstOrDefault();
            ViewBag.GV_NOICT_DVGTVT_MAs = _db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"
                                                                && o.DVGTVT_MA == view.GV_NOICT_DVGTVT_MA).Select(x => x.DVGTVT_TEN).FirstOrDefault();
            ViewBag.GV_NOICT_DVHC_MAs = _db.Filter<SDONVIHC>(o => o.DVHC_MA == view.GV_NOICT_DVHC_MA).Select(x => x.DVHC_TEN).FirstOrDefault();
            ViewBag.GV_HTDTAO_MAs = _db.Filter<SHTDAOTAO>(o => o.HTDT_ACTIVE == "1").Select(x => x.HTDT_MOTA).FirstOrDefault();
            ViewBag.GV_HTTUYENDUNGs = "CT.HĐ";
        }
        #endregion

        #region Chỉnh sửa giấy phép
        public ActionResult _Edit(string id1, string id2)
        {
            string _GV_MA = id1;
            string _GV_DVGTVT_MA = id2;
            CrudModelDCPGV model = Singleton<GPS_DAL.GPS_CapPhepDAO>.Inst.GPS_GETEDIT_DCPGV(_GV_DVGTVT_MA, _GV_MA);
            if (model != null)
            {
                return PartialView("_Edit", model);
            }
            else
            {
                ViewBag.MessengerAler = "Không tìm thấy lịch sử cấp phép cho giáo viên này! Yêu cầu bạn thêm mới!";
                return PartialView("_Alert", model);
            }
        }

        [HttpPost]
        public ActionResult _Edit(CrudModelDCPGV model, FormCollection form)
        {
            if (ModelState.IsValid)
            {
                Singleton<GPS_DAL.GPS_CapPhepDAO>.Inst.GPS_DCPGV_UPDATE(model);
                string url = Url.Action("Index", "GPS_GiayPhepGiaoVien");
                return Json(new { success = true, url = url });
            }
            else
            {
                return PartialView("_Edit", model);
            }
        }

        #endregion

        public ActionResult _Create(string id1, string id2)
        {
            string _GV_MA = id1;
            string _GV_DVGTVT_MA = id2;
            CrudModelDCPGV model = new CrudModelDCPGV();
            DGV _model = _db.Filter<DGV>(x => x.GV_DVGTVT_MA == _GV_DVGTVT_MA && x.GV_MA == _GV_MA).FirstOrDefault();
            model.DCP_GV_MA = _model.GV_MA;
            model.DCP_GV_DVGTVT_MA = _model.GV_DVGTVT_MA;
            model.DCP_GV_DVGTVT_TEN = _db.GetOne<SDONVIGTVT>(x => x.DVGTVT_MA == _GV_DVGTVT_MA).DVGTVT_TEN;
            model.DCP_GV_HANGDT_MA = _model.GV_HANGDT_MA;
            model.DCP_GV_HODEM = _model.GV_HODEM;
            model.DCP_GV_TEN = _model.GV_TEN;
            model.DCP_GV_NGAYSINH = _model.GV_NGAYSINH;
            model.DCP_GV_ANHCD = _model.GV_ANHCD;
            model.DCP_GV_SOCMT = _model.GV_SOCMT;
            //model.DCP_GV_SOQD_GCN = _model.GV_SOQD_GCN;
            //model.DCP_GV_NGAYQD_GCN = _model.GV_NGAYQD_GCN;
            model.DCP_GV_HOTEN = _model.GV_HODEM + " " + _model.GV_TEN;
            return PartialView("_Create", model);
        }

        [HttpPost]
        public ActionResult _Create(CrudModelDCPGV model, FormCollection form)
        {
            if (ModelState.IsValid)
            {
                Singleton<GPS_DAL.GPS_CapPhepDAO>.Inst.GPS_DCPGV_INSERT(model);
                string url = Url.Action("Index", "GPS_GiayPhepGiaoVien");
                return Json(new { success = true, url = url });
            }
            else
            {
                return PartialView("_Edit", model);
            }
        }

        #region xuất giấy chứng nhận

        public ActionResult Export(string _DCP_GV_MA, string _DCP_GV_DVGTVT_MA, string _DCP_GV_DVGTVT_TEN, string _DCP_GV_SOQD
            , string _DCP_GV_NGAYQD, string _DCP_GV_NGAYSINH, string _DCP_GV_SOCMT, string _DCP_GV_SOQD_GCN
            , string _DCP_GV_NGAYQD_GCN, string _DCP_NGAYDT_TU, string _DCP_NGAYDT_DEN)
        {

            try
            {
                DGV model = _db.GetOne<DGV>(x => x.GV_MA == _DCP_GV_MA && x.GV_DVGTVT_MA == _DCP_GV_DVGTVT_MA);
                CrudModelDCPGV _model = new CrudModelDCPGV();
                _model.DCP_GV_MA = _DCP_GV_MA;
                _model.DCP_GV_DVGTVT_MA = _DCP_GV_DVGTVT_MA;
                _model.DCP_GV_DVGTVT_TEN = _DCP_GV_DVGTVT_TEN;
                _model.DCP_GV_SOQD = _DCP_GV_SOQD;
                _model.DCP_GV_NGAYQD = _DCP_GV_NGAYQD == null ? (DateTime?)null : DateTime.Parse(_DCP_GV_NGAYQD);
                _model.DCP_GV_NGAYSINH = _DCP_GV_NGAYSINH == null ? (DateTime?)null : DateTime.Parse(_DCP_GV_NGAYSINH);
                _model.DCP_GV_SOCMT = _DCP_GV_SOCMT;
                _model.DCP_GV_SOQD_GCN = _DCP_GV_SOQD_GCN;
                _model.DCP_GV_NGAYQD_GCN = _DCP_GV_NGAYQD_GCN == null ? (DateTime?)null : DateTime.Parse(_DCP_GV_NGAYQD_GCN); ;
                _model.DCP_NGAYDT_TU = _DCP_NGAYDT_TU == null ? (DateTime?)null : DateTime.Parse(_DCP_NGAYDT_TU);
                _model.DCP_NGAYDT_DEN = _DCP_NGAYDT_DEN == null ? (DateTime?)null : DateTime.Parse(_DCP_NGAYDT_DEN);
                _model.DCP_GV_HODEM = model.GV_HODEM + " " + model.GV_TEN;
                byte[] contentFile = null;
                contentFile = GPS.ENTITYS.EXPORTWORD_DCPGV.EXPORT_DCPGV(Server.MapPath("~/Templates/GPS_Word/GP_ChungNhanGiaoVien.docx"), _model, "DocxGenericReport");
                string nameOutput = "GiayChungNhan_" + _model.DCP_GV_HODEM + "_" + DateTime.Now.ToShortDateString() + ".docx";
                return File(contentFile, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", nameOutput);
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("ErrorMsg", ex.Message);
                return RedirectToAction("Index");
            }
        }

        #endregion
    }

}